package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.adapter.ClueAdapter;
import palmtop.com.palmtop.adapter.TaskAdapter;
import palmtop.com.palmtop.event.CluelistSyncEvent;
import palmtop.com.palmtop.event.MainSyncEvent;
import palmtop.com.palmtop.model.Clue;
import palmtop.com.palmtop.model.Task;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

public class ClueListActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.clueList)
    ListView clueList;
    @BindView(R.id.footBtn)
    Button footBtn;

    String userid;
    private ClueAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clue_list, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(ClueListActivity.this);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onListSyncEvent(CluelistSyncEvent event){
        initView();
    }

    private void initView() {
        mtitle.setText("线索列表");
        footBtn.setText("新增线索");
        AppPreferences appPreferences = new AppPreferences(ClueListActivity.this);
        userid = appPreferences.getString("userid","");
        RequestParams params = new RequestParams(Config.SERVER_URL + "clue/getByPuserid");
        params.addBodyParameter("reportid",userid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<Clue>>() {
                }.getType();
                ArrayList<Clue> list = gson.fromJson(result,listType);
                adapter = new ClueAdapter(list,ClueListActivity.this);
                clueList.setAdapter(adapter);
                /*adapter = new TaskAdapter(list,ClueListActivity.this);
                histaskList.setAdapter(adapter);*/
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {}
            @Override
            public void onCancelled(CancelledException cex) {}
            @Override
            public void onFinished() {}
        });

    }

    @OnClick(R.id.footBtn)
    public void onViewClicked() {
        Intent i = new Intent();
        i.setClass(this, AddClueActivity.class);
        startActivity(i);
    }
}
